package com.itheima.controller;

import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
	@Autowired
	private SetmealService setmealService;

	// 条件查询套餐信息
	@PostMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
		PageResult pageResult = setmealService.findPage(queryPageBean);
		return pageResult;
	}

	// 新增套餐
	@PostMapping()
	public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds) {
		try {
			setmealService.add(setmeal,checkgroupIds);
		}catch (Exception e){
			e.printStackTrace();
			return Result.success("新增套餐失败");
		}
		return Result.success("新增套餐成功");
	}

	// 套餐图片上传
	@PostMapping("/upload")
	public Result imageUpload(@RequestParam("imgFile") MultipartFile imgFile) {
		// 获取原始文件名
		String originalFilename = imgFile.getOriginalFilename();
		int lastIndexOf = originalFilename.lastIndexOf(".");
		// 获取文件后缀
		String suffix = originalFilename.substring(lastIndexOf - 1);
		// 使用UUID随机产生文件名称，防止同名文件覆盖
		String fileName = UUID.randomUUID().toString() + suffix;
		try{
			QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
		} catch (IOException e) {
			e.printStackTrace();
			return Result.error(MessageConstant.PIC_UPLOAD_FAIL);
		}
		return Result.success(fileName,"图片上传成功");
	}

	/*@GetMapping("/download")
	public void download(String name, HttpServletResponse response){
		try {
			// 以jpg的形式发送到客户端的响应的内容类型
			response.setContentType("image/jpeg");
			//  输入流，通过输入流读取文件内容
			FileInputStream fileInputStream =
					new FileInputStream(new File(path,name));
			ServletOutputStream outputStream = response.getOutputStream();
			// 流复制 输入流-->输出流
			IOUtils.copy(fileInputStream,outputStream);
			// 流关闭
			fileInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	// 删除套餐
	@DeleteMapping("/{id}")
	public Result delete(@PathVariable Integer id) {
		try {
			setmealService.delete(id);
		}catch (Exception e){
			e.printStackTrace();
			return Result.error("该套餐正在被使用，不能删除");
		}

		return Result.success("删除套餐成功");
	}

	// 根据id查询套餐
	@GetMapping("/{id}")
	public Result getSetmealById(@PathVariable Integer id) {
		Setmeal setmeal = setmealService.getSetmealById(id);
		return Result.success(setmeal, "查询套餐数据成功");
	}

	// 根据套餐id查询对应的检查组id集合
	@GetMapping("/checkgroup/{id}")
	public Result getCheckgroupId(@PathVariable Integer id) {
		List<Integer> list = setmealService.getCheckGroupId(id);
		return Result.success(list, "查询套餐数据成功");
	}

	// 编辑套餐
	@PutMapping()
	public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
		setmealService.update(setmeal,checkgroupIds);
		return Result.success(MessageConstant.EDIT_USER_SUCCESS);
	}

}
